package com.ztjava.common.tool.push;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Ret;
import com.ztjava.core.Constant;
import com.ztjava.core.tool.ViewTool;
/****
 * 云片网短信接口
 * @author Administrator
 * https://www.yunpian.com/
 */
public class SmsYunPianImp implements PushInterface{
	private static Logger logger = Logger.getLogger(SmsYunPianImp.class);
	private final static String URL = "https://sms.yunpian.com/v2/sms/single_send.json";
	private final static String apiKey = "COMMON_PUSH_YUNPIAN_APIKEY";
	@Override
	public Ret push(String to,String content) {
		Map<String,String> param = new HashMap<String, String>();
		param.put("mobile", to);
		param.put("text", content);
		param.put("apikey", ViewTool.param(apiKey));
		
		String result = "";
		try{
			result = HttpKit.post(URL, param, null);
		}catch (Exception e){
			e.printStackTrace();
			logger.error("云片短信发送失败："+result);
			return Ret.create(Constant.RESULT, Constant.ERROR).set(Constant.MESSAGE, "短信发送失败！");
		}
		JSONObject jsonObject = JSONObject.parseObject(result);
		if(jsonObject.get("code")!=null&&jsonObject.getInteger("code").equals(0)){
			return Ret.create(Constant.RESULT, Constant.SUCCESS).set(Constant.MESSAGE, jsonObject.getString("msg"));
		}else {
			return Ret.create(Constant.RESULT, Constant.ERROR).set(Constant.MESSAGE, jsonObject.getString("msg"));
		}
	}

}
